//
//  YCModalViewController.m
//  DemoTransition
//
//  Created by yunsheng on 15/7/23.
//  Copyright (c) 2015年 yunsheng. All rights reserved.
//

#import "YCModalViewController.h"

#import "YCVCsPresentTransition.h"
#import "YCVCsDismissTransition.h"

#import "YCModalDetailViewController.h"

@interface YCModalViewController ()<UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) UIPercentDrivenInteractiveTransition *percentDrivenTransition;

@end

@implementation YCModalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.transitioningDelegate = self;
    
    [self addScreenEdgePanGestureRecognizerWithView:self.view withRectEdge:UIRectEdgeRight];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIViewControllerTransitioningDelegate

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [[YCVCsPresentTransition alloc] init];
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [[YCVCsDismissTransition alloc] init];
}

-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator
{
    return self.percentDrivenTransition;
}

-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator
{
    return self.percentDrivenTransition;
}

#pragma mark - GestureRecognizer

- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)ges
{
    CGFloat progress = fabs([ges translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);
    
    if (ges.state == UIGestureRecognizerStateBegan) {
        self.percentDrivenTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
        if (ges.edges == UIRectEdgeRight) {
            [self performSegueWithIdentifier:@"segueModalDetail" sender:nil];
        } else if(ges.edges == UIRectEdgeLeft) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } else if(ges.state == UIGestureRecognizerStateChanged) {
        [self.percentDrivenTransition updateInteractiveTransition:progress];
    } else if(ges.state == UIGestureRecognizerStateEnded || ges.state == UIGestureRecognizerStateCancelled) {
        if (progress > 0.5) {
            [self.percentDrivenTransition finishInteractiveTransition];
        } else {
            [self.percentDrivenTransition cancelInteractiveTransition];
        }
        
        self.percentDrivenTransition = nil;
    }
}

#pragma mark - method

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueModalDetail"]) {
        YCModalDetailViewController *vc = segue.destinationViewController;
        vc.transitioningDelegate = self;
        [self addScreenEdgePanGestureRecognizerWithView:vc.view withRectEdge:UIRectEdgeLeft];
    }
    
    [super prepareForSegue:segue sender:sender];
}

- (void)addScreenEdgePanGestureRecognizerWithView:(UIView *)view withRectEdge:(UIRectEdge) rectEdge
{
    UIScreenEdgePanGestureRecognizer *ges = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
    ges.edges = rectEdge;
    [view addGestureRecognizer:ges];
}

@end
